package service.impl;

import dao.ClazzMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import pojo.ClazzM;
import service.ClazzService;

import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    ClazzMapper clazzMapper;

    @Override
    public List<ClazzM> getAll() {
        return clazzMapper.getAll();
    }

    @Override
    public ClazzM getOne(int clazzId) {
        return clazzMapper.getOne(clazzId);
    }

    @Override
    public Integer update(ClazzM c) {
       return clazzMapper.update(c);
    }



    @Override
    public Integer add(ClazzM c) {
        log.info("c="+c);
        if(c.getId() == null){
            log.warn("ClazzM缺少id,随机生成中");
            c.setId(UUID.randomUUID().toString());
        }
       return clazzMapper.add(c);
    }

    @Override
    public Integer del(String id) {
       return clazzMapper.del(id);
    }

    @Override
    public List<ClazzM> getClassByArray(Integer currPage, Integer pageSize) {
        List<ClazzM> clazzMS = clazzMapper.getAll();
        int firstIndex = (currPage -1 )*pageSize;
        int lastIndex = currPage * pageSize;
        if((clazzMS.size()-firstIndex)<pageSize){
            clazzMS =clazzMS.subList(firstIndex,clazzMS.size());
        }
        try {
            clazzMS = clazzMS.subList(firstIndex, lastIndex);
        }catch (Exception e){
            e.printStackTrace();
        }
        return clazzMS;
    }
}
